home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / include / wcedb.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  18.9 KB  |  541 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11.  
  12. #ifndef __WCEDB_H__
  13. #define __WCEDB_H__
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // Wrapper class for CEPROPVAL structure
  17. // Implementation file: wcedb.cpp
  18. /////////////////////////////////////////////////////////////////////////////
  19.  
  20. // Note: you'll see references to "sort" properties.  They contain the
  21. // application-defined identifier, the CEVT_* datatype, but do not contain
  22. // any data.  Furthermore, it also contains a set of flags to specify
  23. // sorting information.
  24.  
  25. // Properties have application-defined identifiers, labelled wIdent
  26. class CCeDBProp : public CObject
  27. {
  28.     DECLARE_DYNAMIC(CCeDBProp);
  29.  
  30. public:
  31.     // Enumerations
  32.     // used in the construction of a CePropVal or a Blob prop to tell it
  33.     // to allocate new memory to store the data passed into the constructor
  34.     enum enCopyMode
  35.     {
  36.         NoCopy = 0,
  37.         Copy = 1
  38.     };
  39.         
  40.     // Wraps the CEVT_* data types, used in construction.  It is primarily
  41.     // used when constructing sort properties.
  42.     enum enType
  43.     {
  44.         Type_Short = 0,
  45.         Type_UShort,
  46.         Type_Long,
  47.         Type_ULong,
  48.         Type_Filetime,
  49.         Type_String,
  50.         Type_Blob
  51.     };
  52.  
  53.     // Wraps the CEDB_SORT_* flags, used when creating "sort" Properties..
  54.     enum enSort
  55.     {
  56.         Sort_Ascending       = 0,
  57.         Sort_Descending      = CEDB_SORT_DESCENDING,
  58.         Sort_CaseInsensitive = CEDB_SORT_CASEINSENSITIVE,
  59.         Sort_UnknownFirst    = CEDB_SORT_UNKNOWNFIRST,
  60.         Sort_GenericOrder    = CEDB_SORT_GENERICORDER
  61.     };
  62.  
  63.     // Public data
  64.     CEPROPVAL m_CePropVal;  // See Ce Database docs
  65.     WORD m_wSortFlags;       // if this is a "sort" property, this contains
  66.                              // the flags doc'd in SORTORDERSPEC
  67.  
  68.     // Constructors/destructors
  69.     CCeDBProp();  // default constructor
  70.     
  71.     // ctor for creating sort properties.
  72.     CCeDBProp(enum enType nType,WORD wIdent = 0,
  73.                WORD wSortFlags = Sort_Ascending);
  74.  
  75.     // ctor for short (CEVT_I2)
  76.     CCeDBProp(short iVal,WORD wIdent = 0);
  77.  
  78.     // ctor for unsigned short (CEVT_UI2)
  79.     CCeDBProp(USHORT uiVal,WORD wIdent = 0);
  80.  
  81.     // ctor for long (CEVT_I4)
  82.     CCeDBProp(long lVal,WORD wIdent = 0);
  83.  
  84.     // ctor for unsigned long (CEVT_UI4)
  85.     CCeDBProp(ULONG ulVal,WORD wIdent = 0);
  86.  
  87.     // ctor for FILETIME (CEVT_FILETIME)
  88.     CCeDBProp(FILETIME filetime,WORD wIdent = 0);
  89.  
  90.     // ctor for LPWSTR (CEVT_LPWSTR).  Copy means that new memory
  91.     // is allocated to store the passed in string.  NoCopy means
  92.     // that this prop will point to the existing string, but won't
  93.     // own it.
  94.     CCeDBProp(LPWSTR lpwstr,WORD wIdent = 0,enCopyMode nMode = Copy);
  95.     
  96.     // ctor for CEBLOB (CEVT_CEBLOB).  Copy/Nobody works the same
  97.     // way as above but with blobs instead of strings.
  98.     CCeDBProp(CEBLOB blob,WORD wIdent = 0,enCopyMode nMode = Copy);
  99.     
  100.     // destructor
  101.     virtual ~CCeDBProp();
  102.  
  103.     // copy constructor
  104.     CCeDBProp(CCeDBProp& prop);
  105.  
  106.     // assignment operator 
  107.     CCeDBProp& operator=(const CCeDBProp& prop);
  108.  
  109.  
  110.     // Get/Set operations
  111.  
  112.     // The datatype (a CEVT_*) value. 
  113.     // Note: in the private section there's corresponding calls that get/sets
  114.     // the datatype using the CEVT_*, which is hidden inside the class.
  115.     void SetType(enType nType);
  116.     enType GetType() const;
  117.  
  118.     // The application-defined identifier
  119.     void SetIdent(WORD wIdent);
  120.     WORD GetIdent() const;
  121.  
  122.     // For "sort" properties (whether or not it really is isn't enforced).
  123.     // Uses zero or more flags specified in enumeration above.
  124.     void SetSortFlags(WORD wSortFlags);
  125.     WORD GetSortFlags() const;
  126.  
  127.     // Get/set the short value.  
  128.     // The property datatype must be a short.
  129.     void SetShort(short iVal);
  130.     short GetShort() const; // requires that the propid says this is a short
  131.  
  132.     // Get/set the unsigned short value.  
  133.     // The property datatype must be an unsigned short.
  134.     void SetUShort(USHORT uiVal);
  135.     USHORT GetUShort() const; // requires ushort prop
  136.  
  137.     // Get/set the long value.  
  138.     // The property datatype must be a long.
  139.     void SetLong(long iVal);
  140.     long GetLong() const;   // requires long prop 
  141.  
  142.     // Get/set the unsigned long value.  
  143.     // The property datatype must be an unsigned long.
  144.     void SetULong(ULONG ulVal);
  145.     ULONG GetULong() const; // requires ulong prop
  146.  
  147.     // Get/set the filetime value
  148.     // The property datatype must be a filetime.
  149.     void SetFiletime(FILETIME filetime);
  150.     FILETIME GetFiletime() const; // requires filetime prop
  151.     
  152.     // Get/set the string value, with the Copy flag described earlier.
  153.     // The property datatype must be a string.
  154.     // The value returned is the same string as pointed to by the property,
  155.     // not a copy.
  156.     void SetString(LPWSTR lpwstr,UINT nMode = Copy);
  157.     LPWSTR GetString() const;   // requires string prop
  158.  
  159.     // Get/set the blob value, with the Copy flag described earlier.
  160.     // The property datatype must be a string.
  161.     // The returned blob is the same one used by the property, not a copy.
  162.     void SetBlob(CEBLOB blob,UINT nMode = Copy);
  163.     CEBLOB GetBlob() const;    // requires blob prop
  164.  
  165. private:
  166.     BOOL m_bOwnsStringOrBlob; 
  167.     // m_bOwnsStringOrBlob applies only to string and blob properties.
  168.     // It is FALSE if the data is stored in a common buffer owned by a
  169.     // CCeDBRecord, as is the case when reading a whole record from
  170.     // CeReadRecordProps. 
  171.     BOOL m_bDeletePtr; // if a record allocates its property (and points 
  172.     // to it) this property needs to be deleted.  This is independent from 
  173.     // m_bOwnsStringOrBlob
  174.     CEPROPID GetPropid() const; 
  175.     void   SetCEVT(WORD wType); 
  176.     WORD   GetCEVT() const;      
  177.     static enType CEVTtoType(WORD wType);
  178.     static WORD   TypeToCEVT(enType nType);    
  179.     void   InitProp(); // Used in construction of the object
  180.     void   FreeProp(); // Deletes memory used by string or blob properties
  181.                    // but doesn't delete this C++ object.};
  182.  
  183.     // (ctor for creating a CCeDBProp based on an existing
  184.     // CEPROPVAL (which is completely wrapped from the user's pt. of view).
  185.     // The Copy flag applies to LPWSTR and CEBLOB properties
  186.     CCeDBProp(CEPROPVAL val,enCopyMode nMode = Copy); 
  187.  
  188.     friend class CCeDBDatabase; // grants access to private members
  189.     friend class CCeDBRecord;   // grants access to private members
  190. };
  191. /////////////////////////////////////////////////////////////////////////////
  192.  
  193.  
  194.  
  195.  
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Wrapper class for and array of CCeDBProp (which is basically the 
  198. //   CEPROPVAL structure)
  199. // Implementation file: wcedb.cpp
  200. /////////////////////////////////////////////////////////////////////////////
  201.  
  202. class CCeDBRecord
  203. {
  204.     friend class CCeDBDatabase; // grants access to private members
  205.  
  206. public:
  207.     
  208.     // Public data
  209.     CObArray m_pPropArray; // the array of properties
  210.  
  211.     // Constructor/destructor
  212.     CCeDBRecord();
  213.     virtual ~CCeDBRecord();
  214.  
  215.     // copy constructor
  216.     CCeDBRecord(CCeDBRecord& rec);
  217.  
  218.     // assignment operator
  219.     CCeDBRecord& operator=(const CCeDBRecord& rec);
  220.  
  221.  
  222.     // Operations
  223.  
  224.     // Adds a property to this record (the CCeDBRecord object, not the
  225.     // database's representation)
  226.     BOOL AddProp(CCeDBProp* pProp);
  227.  
  228.     // Add more than one prop to this record.  Takes in a regular array
  229.     // of props and a size.
  230.     BOOL AddProps(CCeDBProp* pPropArray,int nNumProps);
  231.  
  232.     // Deletes a property from the record (again, just the class's copy. 
  233.     // If you call WriteCurrRecord, it'll delete the extra props there
  234.     // at that time.)  Note the property is referred to by the
  235.     // application-defined wIdent
  236.     void DeleteProp(WORD wPropIdent); // decrements array size by 1
  237.     
  238.     // Like DeleteProp() but for all properties. 
  239.     void DeleteAllProps();
  240.  
  241.  
  242.     // Queries
  243.  
  244.     // Returns the number of properties in the record.
  245.     int  GetNumProps() const;
  246.  
  247.     // Retrieves a property based on the application-defined wIdent
  248.     CCeDBProp* GetPropFromIdent(WORD wPropIdent) const;
  249.  
  250.     // Retrieves a property based on its index (used for looping up
  251.     // to the GetNumProps())
  252.     CCeDBProp* GetPropFromIndex(int nIndex) const;
  253.     
  254.     // Other operarions.  
  255. private:
  256.     BYTE *m_pbBuffer; // Returned by CeReadRecordProps() (see doc).  
  257.                       // This buffer will be owned by the class, and will 
  258.                       // be freed on Delete() and ~CCeDBRecord().
  259.     void FreeRecord(); // deletes all props, frees m_pbBuffer,
  260.                        // and sets array size to 0
  261. };
  262.  
  263. /////////////////////////////////////////////////////////////////////////////
  264.  
  265.  
  266.  
  267.  
  268. /////////////////////////////////////////////////////////////////////////////
  269. // The Ce Database class
  270. // Implementation file: wcedb.cpp
  271. /////////////////////////////////////////////////////////////////////////////
  272.  
  273. class CCeDBDatabase : public CObject
  274. {
  275.     DECLARE_DYNAMIC(CCeDBDatabase);
  276.  
  277. public:
  278.  
  279.     // Data
  280.     // These members are public in the case that someone wants to call
  281.     // the API directly.
  282.     CEOID  m_CEOID;  // object store id as doc'd by Ce Database API
  283.     HANDLE m_hHandle; // handle returned from CeOpenDatabase() (ditto)      
  284.     BOOL   m_bAutoSeekNext; // flag to call SeekNext() after reading and 
  285.                               // writing records, default is FALSE 
  286.     BOOL   m_bEOF;          // End-Of-File flag, set to TRUE only by SeekNext().
  287.                             // Internally, this value is used only by ReadNextRecord().
  288.  
  289.     // Constructor/destructor
  290.     CCeDBDatabase();      
  291.     virtual ~CCeDBDatabase();
  292.  
  293.     // Basic operations
  294.  
  295.     // This creates a peg database, given a object store name, but doesn't open it. 
  296.     // The return value are the same as by CeCreateDatabase().  If the db already
  297.     // exists, it is NOT recreated.
  298.  
  299.     // dwIdent is the application-defined identifier (the Ce Database API calls
  300.     // this dwDbaseType, but I didn't like it for several reasons... 
  301.     // 1. it looks funny,
  302.     // 2. type implies structure, which may not be the case (after all, it's app-defined)
  303.     // 3. it's consistent with the app-defined wIdent for properties
  304.     
  305.     // The two sort parameters wrap the SORTORDERSPEC with an array of "sort" properties.  
  306.     // (A sample illustrates usage)
  307.     // Returns the new pegoid, or null of there's an error. Use GetLastError().
  308.     CEOID Create(LPCWSTR szName,
  309.                   DWORD  dwIdent = 0,      // Note: this can be set later with SetType()
  310.                   int    nNumSortProps = 0, // Note: these can be set later with
  311.                   const CCeDBProp* pSortProps = NULL); // SetSortOrder()
  312.  
  313.     // This opens the database given the object store name
  314.     // Optionally you can give it a "sort" property that will affect the seek order. 
  315.     // (All considerations such as efficiency, behavior, etc, are the same as the
  316.     // Ce Database API.
  317.     // The nofify CWnd parallels the CeDatabaseOpen hWndNotify argument.
  318.     // Returns success of the open. Use GetLastError()
  319.     BOOL Open(LPCWSTR szName,
  320.               const CCeDBProp* pKeyProp = NULL,
  321.               const CWnd* pWndNotify = NULL);
  322.  
  323.     // This opens the database given the object store id (returned from Create(),
  324.     // CCeDBEnumeration::Next(), or elsewhere)
  325.     // Returns success of the Open.  Use GetLastError()
  326.     BOOL Open(CEOID CeOID,
  327.               const CCeDBProp* pKeyProp = NULL,
  328.               const CWnd* pWndNotify = NULL);
  329.  
  330.     // This closes the database.  Note that closing is automatically done upon
  331.     // destruction of the object or when Delete() is called.
  332.     // Returns success of the close.
  333.     BOOL Close();  // Optional for user.  Automatically called by dtor.
  334.  
  335.     // Removes database from object store, closing if necessary.
  336.     // Doesn't delete this C++ object, but it resets 
  337.     // it so nothing further can be done with it.
  338.     // Returns success of the delete.  Use GetLastError().
  339.     BOOL Delete(); 
  340.            
  341.  
  342.     // Static information functions (you can invoke these functions without a 
  343.     // database object.)  (If they weren't static then you'd have a chicken-and-
  344.     // egg problem or you'd have to make them global scope).
  345.     // Note: this doesn't check to see if the objet store object is a database.
  346.     static BOOL Exists(LPCWSTR szName);  // object store name
  347.     static BOOL Exists(CEOID CeOID);   // object store id
  348.  
  349.  
  350.     // Seeking operations (very close to SDK). Each return the new pegoid for 
  351.     // the record seeked to, or NULL if none found.  The current seek position 
  352.     // affects the record being written to and read from.
  353.     // GetLastError() can be used to get errors.
  354.     
  355.     // This seeks the nth record (zero indexed) from the top or bottom of the
  356.     // database.  Note, the sort order is in effect for all seeks.
  357.     CEOID SeekToIndex(DWORD dwCurrIndex,BOOL bFromEnd = FALSE);
  358.  
  359.     // Seeks the first record.
  360.     CEOID SeekFirst() { return SeekToIndex(0);      }
  361.     
  362.     // Seeks the last record.
  363.     CEOID SeekLast()  { return SeekToIndex(0,TRUE); }
  364.  
  365.     // Seeks the next record.  This is automatically called when calling 
  366.     // ReadCurrRecord() and m_bAutoSeekNext == TRUE. 
  367.     CEOID SeekNext();
  368.  
  369.     // Seeks previous record.
  370.     CEOID SeekPrev();
  371.  
  372.     // Seeks (or finds) the record specified by the id
  373.     CEOID SeekToRecord(CEOID CeOID);
  374.  
  375.     // Seeks for the first record with a property that matches the given property
  376.     CEOID SeekFirstEqual(CCeDBProp& Prop);
  377.  
  378.     // Seeks the next one...
  379.     CEOID SeekNextEqual(CCeDBProp& Prop);
  380.  
  381.     // See SDK docs for the behavior of the next two... it's a little confusing.
  382.     CEOID SeekValueSmaller(CCeDBProp& Prop);
  383.     CEOID SeekValueGreater(CCeDBProp& Prop);
  384.  
  385.     // Returns the current index from the top of the database (respective to the
  386.     // current sort order). Zero-based.
  387.     DWORD  GetCurrIndex() const;
  388.  
  389.     // Returns the id of the current record, NULL if none pointed to.
  390.     CEOID GetCurrRecord() const;
  391.  
  392.  
  393.  
  394.     // Read/write operations. 
  395.     
  396.     // Reads the current record, and fills in the record passed in (any properties
  397.     // contained therein are deleted, and previous memory is freed).  Reading a subset
  398.     // of a record (recommended by the SDK docs for efficiency reasons) is accomplished
  399.     // by passing in an array of properties.  Properties don't need values, so use dummy
  400.     // values of the correct type when constructing them.  A sample illustrates this.
  401.     // Not passing in the array (or passing 0) will read the whole record.
  402.     // See SeekNext() on autoseeking behavior.
  403.     // Returns success.  Use GetLastError()
  404.     BOOL ReadCurrRecord(CCeDBRecord* pRecord,
  405.                          int nNumFilterProps = 0, // 0 means all
  406.                          CCeDBProp *PropFilterArray = NULL);
  407.  
  408.     // The following 4 functions are implemented as inline so we don't incur the
  409.     // cost of an unnecessary function call.  These broken-down calls are considerably more 
  410.     // intuitive than the one-size-fits-all WriteRecordProps (which is even more
  411.     // intuitive than the original CeWriteRecordProps!)
  412.  
  413.     // Same idea as ReadCurrRecord, except we're writing a subset instead of reading.
  414.     // Note that writing a subset doesn't remove unwritten properties from the record.
  415.  
  416.     // The sort order is maintained when a key property is changed.
  417.     // Returns success.  Use GetLastError()
  418.     BOOL WriteCurrRecord(CCeDBRecord* pRecord,
  419.                          int nNumFilterProps = 0,
  420.                          CCeDBProp *PropFilterArray = NULL)
  421.     {
  422.         return WriteRecordProps(pRecord,ModifyProps,nNumFilterProps, 
  423.                                 PropFilterArray);
  424.     }
  425.  
  426.  
  427.     // Adds a whole record to the database, in the position determined by the sort order.
  428.     // Returns success.  Use GetLastError()
  429.     BOOL AddRecord(CCeDBRecord* pRecord)
  430.     {
  431.         return WriteRecordProps(pRecord,NewRecord,0,0);
  432.     }
  433.  
  434.     // Deletes the current record (from seeking)
  435.     // Returns success.  Use GetLastError()
  436.     // Note: this is really inefficient!  In order to delete a record, you have to find out what
  437.     // properties are contained in it, which requires a reading of the whole record.  This limitation
  438.     // is more due to the SDK rather than the MFC wrapper implementation
  439.     BOOL DeleteCurrRecord()
  440.     {
  441.         ASSERT(m_hHandle != INVALID_HANDLE_VALUE);
  442.         CEOID RecOID = GetCurrRecord();
  443.         if(RecOID == 0)
  444.             return FALSE;
  445.         return ::CeDeleteRecord(m_hHandle,RecOID);
  446.     }
  447.  
  448.     // Deletes a subset of the properties of the current record by passing in 
  449.     // an array of properties.  As with ReadCurrRecord and WriteCurrRecord, each
  450.     // property will have a dummy value of the appropriate type and application-
  451.     // defined wIdent.
  452.     BOOL DeleteCurrRecordProps(int nNumProps = 0,CCeDBProp *pPropArray = NULL);
  453.  
  454.  
  455.     // Get/Set operations
  456.  
  457.     // Gets the number of records in the db
  458.     WORD  GetNumRecords() const; // no SetNumRecords()
  459.  
  460.     // Gets the size in bytes of the db
  461.     DWORD GetSize() const;       // no SetSize()
  462.  
  463.     // Gets the object store name of the db
  464.     BOOL  SetName(LPCWSTR wszName);
  465.  
  466.     // Sets the object store name of the db
  467.     void  GetName(LPWSTR) const;
  468.  
  469.     // Sets the application-defined dwIdent of the db
  470.     BOOL  SetIdent(DWORD dwIdent);
  471.  
  472.     // Gets the application-defined dwIdent of the db
  473.     DWORD GetIdent() const;
  474.  
  475.     // Sets the modification time of the db
  476.     BOOL  SetLastModified(FILETIME ftLastMotified);
  477.  
  478.     // Gets the modification time of the db
  479.     FILETIME GetLastModified() const;
  480.  
  481.     // Gets the current sort order (an array[0-4] of "sort" properties) 
  482.     void  GetSortProps(int *pnSize,CCeDBProp *SortProps) const;
  483.  
  484.     // Sets the new sort order (an array[0-4) of sort properties.)  Doing
  485.     // this is identical to doing it on Create.  Other considerations
  486.     // described in SDK doc.
  487.     BOOL  SetSortProps(int nSize,const CCeDBProp *SortProps);
  488.  
  489.  
  490.     //  Diagnostics (for CObject-compliancy)
  491.     virtual void AssertValid() const; // overridable
  492.     static void AssertValidDbaseName(LPCWSTR); // note its static
  493.  
  494. private:
  495.     // Private helper functions
  496.     BOOL         SetInfo(const CEDBASEINFO*); 
  497.     CEDBASEINFO GetInfo(BOOL* pbOk) const;       
  498.  
  499.     // Auxiliary write/delete function
  500.     enum { NewRecord, ModifyProps, DeleteProps};
  501.     BOOL WriteRecordProps(CCeDBRecord *pRecord,
  502.                           int nMode /*= CCeDBDatabase::ModifyProps*/,
  503.                            int nNumFilterProps /*= 0*/, 
  504.                           CCeDBProp *PropFilterArray /*= NULL*/);
  505. }; 
  506.  
  507. /////////////////////////////////////////////////////////////////////////////
  508.  
  509.  
  510.  
  511.  
  512. /////////////////////////////////////////////////////////////////////////////
  513. // Enumeration class for Ce Databases
  514. // Implementation file: wcedb.cpp
  515. /////////////////////////////////////////////////////////////////////////////
  516.  
  517. // This class is used to enumerate databases in the object store.  
  518. // Constuction will start the enumeration, and destruction or exhausting
  519. // the database ends it.
  520. class CCeDBEnum
  521. {
  522. public:
  523.     // Starts an enumeration.  If a value other than zero is passed in,
  524.     // only databases with the specified dwIdent are enumerated.
  525.     CCeDBEnum(DWORD dwIdent = 0);
  526.     virtual ~CCeDBEnum();
  527.  
  528.     // Gets the next database in the enumeration, or NULL if no more are
  529.     // found.
  530.     CEOID Next();
  531.  
  532. private:
  533.     HANDLE m_hHandle;
  534. };
  535.  
  536. /////////////////////////////////////////////////////////////////////////////
  537.  
  538.  
  539. #endif // __WCEDB_H__
  540.  
  541.